﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using XYX.Print.Models;
using XYX.Print.Utils;
using Dapper;
namespace XYX.Print.Impl
{
    /// <summary>
    /// 打印记录
    /// </summary>
    public class ScanBarCodePrintRecordImpl
    {
      
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="scanBarCodeRecords"></param>
        /// <returns></returns>
        public bool Insert(List<ScanBarCodePrintRecord> scanBarCodeRecords)
        {

            using (IDbConnection conn = new SqlConnection(DbConnectionManager.ConnString))
            {
                conn.Open();
                foreach (ScanBarCodePrintRecord item in scanBarCodeRecords)
                {
                    conn.Insert(item);
                }
            }
            return false;
        }


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="barCode"></param>
        /// <param name="startDateTime"></param>
        /// <param name="endDateTime"></param>
        /// <returns></returns>
        public IEnumerable<ScanBarCodePrintRecord> GetListPaged(int pageIndex, int pageSize, string barCode, DateTime startDateTime, DateTime endDateTime)
        {
            using (IDbConnection conn = new SqlConnection(DbConnectionManager.ConnString))
            {
                conn.Open();
                return conn.GetListPaged<ScanBarCodePrintRecord>(pageIndex, pageSize, string.Empty, string.Empty);
            }
        }


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="barCode"></param>
        /// <param name="groupCode"></param>
        /// <param name="startDateTime"></param>
        /// <param name="endDateTime"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IEnumerable<ScanBarCodePrintRecord> GetListPaged(int pageIndex, int pageSize, string barCode,string groupCode,  DateTime? startDateTime, DateTime? endDateTime, out int count)
        {
            count = 0;
            using (IDbConnection conn = new SqlConnection(DbConnectionManager.ConnString))
            {
                conn.Open();
                StringBuilder sql = new StringBuilder();
                sql.AppendLine($@"
                                SELECT * FROM ScanBarCodePrintRecord
                                WHERE  1=1  
                               ");
                     
                if (!string.IsNullOrEmpty(barCode))
                {
                    sql.AppendLine($"AND BarCode='{barCode}'");
                }
                if (!string.IsNullOrEmpty(groupCode))
                {
                    sql.AppendLine($"AND GroupCode='{groupCode}'");
                }
                if (startDateTime.HasValue)
                {
                    sql.AppendLine($"AND PrintDateTime>='{startDateTime.Value}'");
                }
                if (endDateTime.HasValue)
                {
                    sql.AppendLine($"AND PrintDateTime<='{endDateTime.Value}'");
                }
                sql.AppendLine($" ORDER BY  CreateDateTime DESC");
                sql.AppendLine($" OFFSET (({pageIndex}-1)*{pageSize}) ROWS  FETCH NEXT {pageSize} ROWS ONLY");

                List <ScanBarCodePrintRecord> list = conn.Query<ScanBarCodePrintRecord>(sql.ToString()).ToList();

                //数量
                StringBuilder sqlCount = new StringBuilder();
                sqlCount.AppendLine($@"
                                SELECT COUNT(1) FROM ScanBarCodePrintRecord
                                WHERE 1=1
                        ");
                if (!string.IsNullOrEmpty(barCode))
                {
                    sqlCount.AppendLine($"AND BarCode='{barCode}'");
                }
                if (!string.IsNullOrEmpty(groupCode))
                {
                    sqlCount.AppendLine($"AND GroupCode='{groupCode}'");
                }
                if (startDateTime.HasValue)
                {
                    sqlCount.AppendLine($"AND PrintDateTime>='{startDateTime.Value}'");
                }
                if (endDateTime.HasValue)
                {
                    sqlCount.AppendLine($"AND PrintDateTime<='{endDateTime.Value}'");
                }
                var countNumber = conn.Query<int?>(sqlCount.ToString());
                if (countNumber != null && countNumber.Any())
                {
                    if (countNumber.FirstOrDefault() >= 1)
                    {
                        count = countNumber.FirstOrDefault().Value;
                    }
                }
                return list;
            }
        }
    }
}
